package pro.cherish.knowledgescattered.configFile.IsReadProperties;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-01 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：TestClassLoader
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-01 cherish
 */
// // FIXME: 2019/11/1
//    参考博客：https://blog.csdn.net/qq_38366063/article/details/83819454
//              https://blog.csdn.net/h2604396739/article/details/83860334

/**
 * 		    //扩展类加载器Main
 *         ClassLoader classLoader = MainTest.class.getClassLoader();
 *         //表示当前线程的类加载器——应用程序类加载器
 *         ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
 *         //—启动类加载器
 *         ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
 */
@Slf4j
public class TestClassLoader {
    public void test01() throws IOException {
/*
        // getClassLoader后已经定位到根路径，正确读取
        InputStream resourceAsStream = TestClassLoader.class.getClassLoader().getResourceAsStream("application.properties");
        pring(resourceAsStream);
        // 空指针异常
        // InputStream resourceAsStream2 = TestClassLoader.class.getClassLoader().getResourceAsStream("/application.properties");
        // pring(resourceAsStream2);
 */
/*

        // 未先获取classLoader，直接getResourceAsStream获取资源，需要加上/ 定位到根路径即可
        InputStream resourceAsStream3 = TestClassLoader.class.getResourceAsStream("/application.properties");
        pring(resourceAsStream3);

*/

/*      使用方式也是一致
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        InputStream resourceAsStream4 = contextClassLoader.getResourceAsStream("application.properties");
        pring(resourceAsStream4);
*/
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        InputStream resourceAsStream5 = systemClassLoader.getResourceAsStream("application.properties");
        // pring(resourceAsStream5);
        Properties pro1 = load(resourceAsStream5);
        log.info(pro1.toString());
        // load的反操作，其中测试配置为注释，如果是xml则是注释标签。两者都是辅助标记作用
        pro1.store(new FileOutputStream(new File("D://test.properties")),"测试配置");
        pro1.storeToXML(new FileOutputStream(new File("D://test.xml")), "测试配置");
        System.out.println("end");
    }

    private Properties load(InputStream in) throws IOException {
        Properties properties = new Properties();
        properties.load(in);
        return properties;
    }
    private void pring(InputStream in) throws IOException {
        byte[] bytes=new byte[1024];
        // 读入多个字节到字节数组中，byteread为一次读入的字节数
        int byteread = 0;
        while ((byteread = in.read(bytes)) != -1) {
            //  System.out.write(tempbytes, 0, byteread);
            String str = new String(bytes, 0, byteread);
            log.info(str);
        }
    }
}
